<?php defined('_JEXEC') or die('Restricted access'); 

$db =& JFactory::getDBO();
$user =& JFactory::getUser();
$user_id = $user->get('id');

if($user->guest) {
	?>
	<h3>Nie masz uprawnień do oglądania tej treści!</h3>
	<?php
} else {

$max_uploads = 2;

?>

<link href="components/com_movieuploader/css/movieuploader.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="components/com_movieuploader/js/swfupload.js"></script>
<script type="text/javascript" src="components/com_movieuploader/js/swfupload.queue.js"></script>
<script type="text/javascript" src="components/com_movieuploader/js/fileprogress.js"></script>
<script type="text/javascript" src="components/com_movieuploader/js/handlers.js"></script>
<script type="text/javascript" src="components/com_movieuploader/js/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
	var swfu;

	window.onload = function() {
		var settings = {
			flash_url : "components/com_movieuploader/js/swfupload.swf",
			upload_url: "components/com_movieuploader/upload.php",
			file_size_limit : "1000 MB",
			file_types : "*.*",
			file_types_description : "All Files",
			file_upload_limit : 10000,
			file_queue_limit : 0,
			custom_settings : {
				progressTarget : "fsUploadProgress",
				cancelButtonId : "btnCancel"
			},
			debug: false,

			// Button Settings
			button_placeholder_id : "spanButtonPlaceholder",
			button_width: "120",
			button_height: "29",
			button_text: '<span class="theFont">Wybierz plik...</span>',
			button_text_style: ".theFont {  }",
			button_text_left_padding: 12,
			button_text_top_padding: 3,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,

			// The event handler functions are defined in handlers.js
			swfupload_loaded_handler : swfUploadLoaded,
			file_queued_handler : fileQueued,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			queue_complete_handler : queueComplete,	// Queue plugin event
			
			// SWFObject settings
			minimum_flash_version : "9.0.28",
			swfupload_pre_load_handler : swfUploadPreLoad,
			swfupload_load_failed_handler : swfUploadLoadFailed
		};

		swfu = new SWFUpload(settings);
     };

	function uploadSuccess(fileObject, serverData, response){
		//alert("fileObject:\n"+JSON.stringify(fileObject)+"\n\nserverData:\n"+JSON.stringify(serverData) ); 
		document.getElementById("resultDiv").innerHTML = "<br><br><strong>Trwa wyliczanie RMSE... Proszę czekać...</strong><br><br>";
		$.post("components/com_movieuploader/computeResult.php",
			{
				fileName: serverData
				//name: "John",
				//time: "2pm"
			},
			function(data) {
				//alert("Data Loaded: " + data);
				document.getElementById("resultDiv").innerHTML = data;
			}
		);
	}

</script>

<h2>Wczytywanie wyników i wyliczenie RMSE</h2>

<?php 
	$query = 'SELECT count(*) FROM '.$db->nameQuote('#__putmovies_rating').' WHERE '.$db->nameQuote('user_id').' = '.$user_id.' AND DATE('.$db->nameQuote('date').') = DATE(NOW())';
	$db->setQuery($query);
	$dbResults = $db->loadResult();
	
	if($dbResults < $max_uploads) {
?>
	<h3>Wykorzystałeś dziś <strong><?php echo $dbResults;?></strong> z <?php echo $max_uploads; ?> pobrań.</h3>
	<div id="resultDiv">
	<p>Kliknij "Wybierz plik...", wybierz plik z wynikami ze swojego komputera i poczekaj na uzyskanie wyniku. Uwaga: może to potrwać kilka minut!</p><br>
	<form id="form1" action="index.php" method="post" enctype="multipart/form-data">

		<div id="divSWFUploadUI">
			<div class="fieldset flash" id="fsUploadProgress">
			<span class="legend">Lista wczytywanych plików</span>
			</div>
			<p id="divStatus">0 Files Uploaded</p>
			<p>
				<span id="spanButtonPlaceholder"></span>
				<input id="btnCancel" type="button" value="Cancel All Uploads" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
			</p>
			<br style="clear: both;" />
		</div>
		<noscript style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px;">
			We're sorry.  SWFUpload could not load.  You must have JavaScript enabled to enjoy SWFUpload.
		</noscript>
		<div id="divLoadingContent" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
			SWFUpload is loading. Please wait a moment...
		</div>
		<div id="divLongLoading" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
			SWFUpload is taking a long time to load or the load has failed.  Please make sure that the Flash Plugin is enabled and that a working version of the Adobe Flash Player is installed.
		</div>
		<div id="divAlternateContent" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
			We're sorry.  SWFUpload could not load.  You may need to install or upgrade Flash Player.
			Visit the <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Adobe website</a> to get the Flash Player.
		</div>
	</form>
	</div>
<?php 
	} else {
?>
	<h3>Wyczerpano dzisiejszy limit wczytywania wyników! Zapraszamy jutro :)</h3>
	<p>Jeden dzień trwa od godziny 00:00 do 23:59.</p>
<?php 
	}


}
?>